//
//  ZYAddEssentialController.m
//  DDD
//
//  Created by Ethan on 2017/10/6.
//  Copyright © 2017年 乾坤. All rights reserved.
//

#import "ZYAddEssentialController.h"

@interface ZYAddEssentialController ()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *inputTextView;
@property (nonatomic,strong) UIButton *sureButton;
@end

@implementation ZYAddEssentialController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"要素";
    self.view.backgroundColor = ColorRGB(204, 204, 204);
    
    
    

    [self setUpSuperView];
    
}


- (void)setUpSuperView{
    
    self.inputTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 15, kScreenWidth - 20, 100)];
    [self.inputTextView  setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.inputTextView ];
    self.inputTextView.delegate = self;
    
    // _placeholderLabel
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"请输入要素";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = ColorRGB(204, 204, 204);
    [placeHolderLabel sizeToFit];
    [self.inputTextView  addSubview:placeHolderLabel];
    self.inputTextView .font = [UIFont systemFontOfSize:15.f];
    self.inputTextView.textColor = ColorRGB(204, 204, 204);
    placeHolderLabel.font = [UIFont systemFontOfSize:15.f];
    [self.inputTextView  setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    
    
    UILabel *placeHolderLabelTow = [[UILabel alloc] initWithFrame:CGRectMake(self.inputTextView.frame.size.width - 80, 80, 80, 20)];
    placeHolderLabelTow.text = @"10字以内";
    
    placeHolderLabelTow.font = [UIFont systemFontOfSize:13];
    placeHolderLabelTow.textColor = ColorRGB(204, 204, 204);
    [self.inputTextView addSubview:placeHolderLabelTow];
    
    
    
    
    
    self.sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.sureButton.frame = CGRectMake(10, kScreenHeight - 180, kScreenWidth - 20, 40);

    [self.sureButton setBackgroundImage:[UIImage imageNamed:@"btn_ensure_defaultt"] forState:(UIControlStateNormal)];
    
    [self.sureButton setTitle:@"保存" forState:(UIControlStateNormal)];
    [self.sureButton setTitleColor:ColorRGB(255, 255, 255) forState:(UIControlStateNormal)];
    [self.view addSubview:self.sureButton];
    
    
    [self.sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
}

#pragma mark --- 保存方法的实现
- (void)sureButtonAction:(UIButton *)sender{
    
    
    if (self.inputTextView.text.length == 0) {
        
        [self createAlertViewWithTitle:nil message:@"请添加要素" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
    }else{
        if (self.isUpDate) {
            [self setNetWork];
        }else{
            
            [self setNetWorkTow];
        }
    }
    
    
    
    
    
    
    
    
}



#pragma mark ---修改方法的实现
- (void)setNetWork{
    
    //http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=updateTemplateContent&id=37&content=好习惯打断点44&type=1
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=updateTemplateContent&id=%@&content=%@&type=%@",self.ID,self.inputTextView.text,self.upDateId];
    NSString *changeUilStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
     NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [NetManager Get:LLBaseURL(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            
            if (self.backBlock) {
                self.backBlock();
            }
            
            [self.navigationController popViewControllerAnimated:YES];
            
            
        }
        

        
        
        
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
}


#pragma mark --- 保存方法的实现
- (void)setNetWorkTow{
    //http://192.168.0.113:8080/StarShine/ys/yxhUser.do?content=方法&templateId=7&action=addTemplateContent&type=2
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?content=%@&templateId=%@&action=addTemplateContent&type=%@",self.inputTextView.text,self.templateId,self.upDateId];
    NSString *changeUilStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [NetManager Get:LLBaseURL(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            
            if (self.backBlock) {
                self.backBlock();
            }
            
            [self.navigationController popViewControllerAnimated:YES];
            
            
        }
        
        
        
        
    } Fail:^(NSError *Error) {
        
    }];
    

    
    
}

- (void)textViewDidChange:(UITextView *)textView{
    
    if (textView.text.length >50) {
        textView.text = [textView.text substringToIndex:10];
       
        [self createAlertViewWithTitle:nil message:@"字数已达上限" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
    }
    
}



@end
